home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / APPLICAT / C050.ZIP / SFSSRC.ZIP / SFS.SRC / AS / AS_FOCUS.C next >
C/C++ Source or Header  |  1991-11-04  |  7KB  |  252 lines

  1. /****************************************************************
  2.  
  3.     as_focus.c      Orbital focus routines
  4.             for astronomy (as) subsystem
  5.  
  6.             Copyright (c) 1991, Ted A. Campbell
  7.  
  8.             Bywater Software
  9.             P. O. Box 4023 
  10.             Duke Station 
  11.             Durham, NC  27706
  12.  
  13.             email: tcamp@hercules.acpub.duke.edu
  14.  
  15.     Copyright and Permissions Information:
  16.  
  17.     All U.S. and international copyrights are claimed by the
  18.     author. The author grants permission to use this code
  19.     and software based on it under the following conditions:
  20.     (a) in general, the code and software based upon it may be 
  21.     used by individuals and by non-profit organizations; (b) it
  22.     may also be utilized by governmental agencies in any country,
  23.     with the exception of military agencies; (c) the code and/or
  24.     software based upon it may not be sold for a profit without
  25.     an explicit and specific permission from the author, except
  26.     that a minimal fee may be charged for media on which it is
  27.     copied, and for copying and handling; (d) the code must be 
  28.     distributed in the form in which it has been released by the
  29.     author; and (e) the code and software based upon it may not 
  30.     be used for illegal activities. 
  31.  
  32. ****************************************************************/
  33.  
  34. #include "stdio.h"
  35. #include "ctype.h"
  36. #include "bw.h"
  37. #include "as.h"
  38.  
  39. #ifdef __STDC__
  40. #include "malloc.h"
  41. #else
  42. extern char * malloc();
  43. #endif
  44.  
  45. #define CHECK_PARAMS
  46.  
  47. /****************************************************************
  48.  
  49.     as_readfd()     read a focal data (fd) file
  50.  
  51. ****************************************************************/
  52.  
  53. as_readfd( fdfile, fstruct )
  54.    char *fdfile;
  55.    struct as_focus *fstruct;
  56.    {
  57.    FILE *data;                          /* data file for read */
  58.    register int c;                      /* counter */
  59.    static char x_buffer[ BW_EBUFSIZE ]; /* buffer to read data */
  60.    char *r;                             /* return value from as_fgets */
  61.  
  62.    /* Send notice */
  63.  
  64.    sprintf( bw_ebuf, "Reading focal datafile %s",
  65.       fdfile );
  66.    bw_message( bw_ebuf );
  67.  
  68.    /***  Try to open the file, return for failure */
  69.  
  70.    if ( ( data = fopen( fdfile, "rb" )) == NULL )
  71.       {
  72.       sprintf( bw_ebuf, "Cannot open focal data file %s",
  73.      fdfile );
  74.       bw_error( bw_ebuf );
  75.       return BW_ERROR;
  76.       }
  77.  
  78.    /***  Begin reading elements from the file */
  79.    /***  0. save fd filename itself */
  80.  
  81.    /* allocate memory for name */
  82.  
  83.    if ( ( fstruct->fdfile = malloc( strlen( fdfile ) + 2 )) == NULL )
  84.       {
  85.       bw_error( "Failed to allocate memory for fd filename. " );
  86.       return BW_ERROR;                          /* error return */
  87.       }
  88.    strcpy( fstruct->fdfile, fdfile );
  89.  
  90.    /***  1. Read name of the focus */
  91.  
  92.    r = as_fgets( x_buffer, 127, data );         /* get a string */
  93.    if ( r == NULL )
  94.       {
  95.       sprintf( bw_ebuf, "Failure to read focal data file %s ", fdfile );
  96.       bw_error( bw_ebuf );
  97.       return BW_ERROR;
  98.       }
  99.  
  100. #ifdef OLD_DEBUG
  101.    fprintf( stderr, "DEBUG: read name \n" );
  102. #endif
  103.  
  104.    for ( c = 0; x_buffer[ c ] >= ' '; ++c )     /* find end of string */
  105.       {
  106.  
  107. #ifdef OLD_DEBUG
  108.    fprintf( stderr, "DEBUG: counting: %3d [char 0x%3x next 0x%3x] \n", 
  109.       c, x_buffer[ c ], x_buffer[ c + 1 ]  );
  110. #endif
  111.  
  112.       }
  113.    x_buffer[ c ] = 0;                           /* terminate with 0 */
  114.  
  115.    /* allocate memory for name */
  116.  
  117.    if ( ( fstruct->name = malloc( strlen( x_buffer ) + 2 )) == NULL )
  118.       {
  119.       bw_error( "Failed to allocate memory for focal data elements. " );
  120.       return BW_ERROR;                          /* error return */
  121.       }
  122.  
  123. #ifdef OLD_DEBUG
  124.    fprintf( stderr, "DEBUG: allocated memory \n" );
  125. #endif
  126.  
  127.    strcpy( fstruct->name, x_buffer );           /* copy name to buffer */
  128.  
  129. #ifdef OLD_DEBUG
  130.    fprintf( stderr, "DEBUG: copied name to buffer \n" );
  131. #endif
  132.  
  133.    /***  2. Read adjective for the focus */
  134.  
  135.    r = as_fgets( x_buffer, 127, data );        /* get a string */
  136.    if ( r == NULL )
  137.       {
  138.       sprintf( bw_ebuf, "Failure to read focal data file %s ", fdfile );
  139.       bw_error( bw_ebuf );
  140.       return BW_ERROR;
  141.       }
  142.  
  143. #ifdef OLD_DEBUG
  144.    fprintf( stderr, "DEBUG: read adjective \n" );
  145. #endif
  146.  
  147.    for ( c = 0; x_buffer[ c ] >= ' '; ++c )    /* find end of string */
  148.       {
  149.  
  150. #ifdef OLD_DEBUG
  151.    fprintf( stderr, "DEBUG: counting: %2d [char 0x%2x next 0x%2x] \n",
  152.       c, x_buffer[ c ], x_buffer[ c + 1 ]  );
  153. #endif
  154.  
  155.       }
  156.  
  157. #ifdef OLD_DEBUG
  158.    fprintf( stderr, "DEBUG: count complete; c = %d \n", c );
  159. #endif
  160.  
  161.    x_buffer[ c ] = 0;                          /* terminate with 0 */
  162.  
  163. #ifdef OLD_DEBUG
  164.    fprintf( stderr, "DEBUG: buffer terminated \n" );
  165. #endif
  166.  
  167.    /* allocate memory for adjective */
  168.  
  169.    if ( ( fstruct->adjective = malloc( strlen( x_buffer ) + 2 )) == NULL )
  170.       {
  171.       bw_error( "Failed to allocate memory for focal data elements. " );
  172.       return BW_ERROR;                          /* error return */
  173.       }
  174.  
  175. #ifdef OLD_DEBUG
  176.    fprintf( stderr, "DEBUG: allocated memory \n" );
  177. #endif
  178.  
  179.    strcpy( fstruct->adjective, x_buffer );      /* copy adjective to buffer */
  180.  
  181. #ifdef OLD_DEBUG
  182.    fprintf( stderr, "DEBUG: copied adjective \n" );
  183. #endif
  184.  
  185.    /***  3. Read radius of the focus */
  186.  
  187.    r = as_fgets( x_buffer, 127, data );
  188.    if ( r == NULL )
  189.       {
  190.       sprintf( bw_ebuf, "Failure to read focal data file %s ", fdfile );
  191.       bw_error( bw_ebuf );
  192.       return BW_ERROR;
  193.       }
  194.  
  195. #ifdef OLD_DEBUG
  196.    fprintf( stderr, "DEBUG: read radius \n" );
  197. #endif
  198.  
  199.    sscanf( x_buffer, "%lf", &fstruct->radius );
  200.  
  201. #ifdef OLD_DEBUG
  202.    fprintf( stderr, "DEBUG: scanned radius \n" );
  203. #endif
  204.  
  205.    /***  4. Read mass of the focus */
  206.  
  207.    r = as_fgets( x_buffer, 127, data );
  208.    if ( r == NULL )
  209.       {
  210.       sprintf( bw_ebuf, "Failure to read focal data file %s ", fdfile );
  211.       bw_error( bw_ebuf );
  212.       return BW_ERROR;
  213.       }
  214.  
  215. #ifdef OLD_DEBUG
  216.    fprintf( stderr, "DEBUG: read mass \n" );
  217. #endif
  218.  
  219.    sscanf( x_buffer, "%lf", &fstruct->mass );
  220.  
  221. #ifdef OLD_DEBUG
  222.    fprintf( stderr, "DEBUG: scanned mass \n" );
  223. #endif
  224.  
  225.    fstruct->mass *= 5.98e27;
  226.  
  227.    /***  5. Read sidereal period of the focus */
  228.  
  229.    r = as_fgets( x_buffer, 127, data );
  230.    if ( r == NULL )
  231.       {
  232.       sprintf( bw_ebuf, "Failure to read focal data file %s ", fdfile );
  233.       bw_error( bw_ebuf );
  234.       return BW_ERROR;
  235.       }
  236.  
  237. #ifdef OLD_DEBUG
  238.    fprintf( stderr, "DEBUG: read sidereal period \n" );
  239. #endif
  240.  
  241.    sscanf( x_buffer, "%lf", &fstruct->sid_day );
  242.  
  243. #ifdef OLD_DEBUG
  244.    fprintf( stderr, "DEBUG: scanned sidereal period \n" );
  245. #endif
  246.  
  247.    fclose( data );                              /* close the fd file */
  248.  
  249.    }
  250.  
  251.  
  252.